package com.ztgf.commodity.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CommodityParamMapper;
import com.ztgf.commodity.models.bo.AttributesQueryBO;
import com.ztgf.commodity.models.bo.CommodityParamNewSaveBO;
import com.ztgf.commodity.models.bo.CommodityParamSaveBO;
import com.ztgf.commodity.models.dto.CommodityParamDTO;
import com.ztgf.commodity.models.dto.CommodityParamValueDTO;
import com.ztgf.commodity.models.service.ICommodityParamService;
import com.ztgf.commodity.models.service.ICommodityParamValueService;
import com.ztgf.commodity.models.vo.CommodityParamVO;
import com.ztgf.commodity.models.vo.CommodityParamValueVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.*;

/**
 * 商品参数信息表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-15 10:12:31
 */
@Slf4j
@DubboService
public class CommodityParamServiceImpl extends ServiceImpl<CommodityParamMapper, CommodityParamDTO> implements ICommodityParamService {

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private ICommodityParamValueService commodityParamValueService;

    @Override
    public boolean add(CommodityParamSaveBO saveBO) {
        CommodityParamDTO dto = new CommodityParamDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public Page<CommodityParamVO> queryPageList(AttributesQueryBO bo) {
        Page<CommodityParamVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<CommodityParamVO> list = baseMapper.queryPageList(page, bo);
        page.setRecords(list);
        return page;
    }

    @Override
    public CommodityParamVO getById(Long id) {
        return baseMapper.getById(id);
    }

    @Override
    public boolean updateState(Long id) {
        CommodityParamDTO dto = this.selectById(id);
        if (dto == null) {
            return false;
        }
        CommodityParamDTO updateDto = new CommodityParamDTO();
        updateDto.setId(dto.getId());
        if (dto.getStatus() == 0) {
            updateDto.setStatus(ONE);
        } else {
            updateDto.setStatus(ZERO);
        }
        return this.updateById(updateDto);
    }

    /**
     * @description:新的添加参数接口
     * @author: chenyu.wang
     * @date: 2020-09-03
     */
    @Transactional
    @Override
    public Result<List<String>> newAdd(String object) {
        Result<List<String>> result = new Result<>();
        Long id = ymlConfig.getPkId();
        CommodityParamNewSaveBO bo = JSONObject.parseObject(object, CommodityParamNewSaveBO.class);
        List<CommodityParamValueVO> attrBOS = JSONObject.parseArray(bo.getValuse(), CommodityParamValueVO.class);
        //保存参数
        CommodityParamDTO dto = new CommodityParamDTO();
        dto.setParamName(bo.getParamName());
        dto.setSort(bo.getSort());
        dto.setStatus(ZERO);
        dto.setIsPublish(1);
        dto.setId(id);
        dto.setNotes(bo.getNotes());
        boolean insert = this.insert(dto);
        if (!insert) {
            result.setCode(CodeEnum.FAIL_100007.getCode());
            result.setMsg(CodeEnum.FAIL_100007.getMsg());
            return result;
        }
        if (isNull(attrBOS)) {
            result.setCode(CodeEnum.SUCCESS.getCode());
            result.setMsg(CodeEnum.SUCCESS.getMsg());
            return result;
        }
        //保存参数属性
        List<CommodityParamValueDTO> attrDTOList = new ArrayList<>();
        for (CommodityParamValueVO attrBO : attrBOS) {
            CommodityParamValueDTO attrDTO = new CommodityParamValueDTO();
            attrDTO.setParamId(id);
            attrDTO.setValueName(attrBO.getValueName());
            attrDTO.setId(ymlConfig.getPkId());
            attrDTOList.add(attrDTO);
        }
        boolean insertBatch = commodityParamValueService.insertBatch(attrDTOList);
        if (!insertBatch) {
            result.setCode(CodeEnum.FAIL_100007.getCode());
            result.setMsg(CodeEnum.FAIL_100007.getMsg());
            return result;
        }
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setMsg(CodeEnum.SUCCESS.getMsg());
        return result;
    }

    /**
     * @description:新的修改
     * @author: chenyu.wang
     * @date: 2020-09-03
     */
    @Transactional
    @Override
    public Result<List<String>> newUpdate(String object) {
        Result<List<String>> result = new Result<>();
        CommodityParamNewSaveBO bo = JSONObject.parseObject(object, CommodityParamNewSaveBO.class);
        List<CommodityParamValueVO> attrBOS = JSONObject.parseArray(bo.getValuse(), CommodityParamValueVO.class);
        //保存规格
        CommodityParamDTO dto = new CommodityParamDTO();
        dto.setParamName(bo.getParamName());
        dto.setSort(bo.getSort());
        dto.setStatus(ZERO);
        dto.setIsPublish(1);
        dto.setId(bo.getId());
        dto.setNotes(bo.getNotes());
        boolean update = this.updateById(dto);
        if (!update) {
            result.setCode(CodeEnum.FAIL_100008.getCode());
            result.setMsg(CodeEnum.FAIL_100008.getMsg());
            return result;
        }
        if (isNull(attrBOS)) {
            result.setCode(CodeEnum.SUCCESS.getCode());
            result.setMsg(CodeEnum.SUCCESS.getMsg());
            return result;
        }
        //保存属性
        CommodityParamValueDTO attrDTO;
        for (CommodityParamValueVO attrBO : attrBOS) {
            attrDTO = new CommodityParamValueDTO();
            if (null == attrBO.getId()) {
                attrDTO.setParamId(bo.getId());
                attrDTO.setValueName(attrBO.getValueName());
                attrDTO.setId(ymlConfig.getPkId());
                commodityParamValueService.insert(attrDTO);
            } else {
                attrDTO.setValueName(attrBO.getValueName());
                attrDTO.setId(attrBO.getId());
                commodityParamValueService.updateById(attrDTO);
            }
        }
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setMsg(CodeEnum.SUCCESS.getMsg());
        return result;
    }

    @Override
    public Page<CommodityParamVO> newQueryPageList(AttributesQueryBO bo) {
        Page<CommodityParamVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<CommodityParamVO> list = baseMapper.queryPageList(page, bo);
        if (!isNull(list)) {
            page.setRecords(list);
            return page;
        }
        Page<CommodityParamValueDTO> valuePage = new Page<>(bo.getPageNo(), bo.getPageSize());
        Wrapper<CommodityParamValueDTO> wrapper = new EntityWrapper<>();
        wrapper.like("value_name", bo.getAttributesName());
        Page<CommodityParamValueDTO> selectPage = commodityParamValueService.selectPage(valuePage, wrapper);
        List<CommodityParamValueDTO> valueList = selectPage.getRecords();
        if (isNull(valueList)) {
            return null;
        }
        List<String> ids = new ArrayList<>();
        for (CommodityParamValueDTO valueDTO : valueList) {
            if (ids.contains(valueDTO.getParamId().toString())) {
                continue;
            } else {
                ids.add(valueDTO.getParamId().toString());
            }
        }
        if (isNull(ids)) {
            return null;
        }
        List<CommodityParamVO> realList = new ArrayList<>();
        for (String id : ids) {
            CommodityParamDTO commodityParamDTO = this.selectById(id);
            if (null != commodityParamDTO) {
                List<CommodityParamValueVO> valueVOList = baseMapper.getByParamId(commodityParamDTO.getId());
                CommodityParamVO paramVO = new CommodityParamVO();
                paramVO.setId(commodityParamDTO.getId());
                paramVO.setParamName(commodityParamDTO.getParamName());
                paramVO.setState(commodityParamDTO.getStatus());
                paramVO.setIsPublish(commodityParamDTO.getIsPublish());
                paramVO.setSort(commodityParamDTO.getSort());
                paramVO.setNotes(commodityParamDTO.getNotes());
                if (!isNull(valueVOList)) {
                    paramVO.setValuse(valueVOList);
                }
                realList.add(paramVO);
            }
        }
        page.setRecords(realList);
        return page;
    }

}
